home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 May / Macworld (1999-05).dmg / Shareware World / Comms & Internet / OTSessionWatcher 2.0 / Developer Extras.sit / Developer Extras / Example / Example.c < prev    next >
C/C++ Source or Header  |  1997-11-19  |  2KB  |  90 lines

  1. #include <OpenTransport.h>
  2. #include <OpenTptInternet.h>
  3. #include <Events.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. static UInt32 gLastTicks = 0;
  8.  
  9. static pascal void MyNotifier(void* contextPtr, OTEventCode code, 
  10.                                        OTResult result, void* cookie)
  11. {
  12.     #pragma unused(contextPtr)
  13.     #pragma unused(result)
  14.     #pragma unused(cookie)
  15.     
  16.     switch (code) {
  17.         case kOTSyncIdleEvent:
  18.             if ( TickCount() > gLastTicks + 10) {
  19.                 printf(".");
  20.                 fflush(stdout);
  21.                 gLastTicks = TickCount();
  22.             }
  23.             break;
  24.         default:
  25.             // do nothing
  26.             break;
  27.     }
  28. }
  29.  
  30. static char *gHTTPServer = "www.stairways.com:80";
  31. static char *gGetHTTPCommand = "GET / HTTP/1.0\15\12\15\12";
  32.  
  33. void main(void)
  34. {
  35.     EndpointRef ep;
  36.     OSStatus err;
  37.     TCall connectCall;
  38.     DNSAddress connectAddr;
  39.     OTResult bytesSent;
  40.     OTResult bytesReceived;
  41.     char ch;
  42.     OTFlags junkFlags;
  43.     
  44.     printf("Hello Cruel World!\n");
  45.     
  46.     err = InitOpenTransport();
  47.     if (err == noErr) {
  48.         ep = OTOpenEndpoint(OTCreateConfiguration("OTSessionWatcher,tcp"), 0, nil, &err);
  49.         if (err == noErr) {
  50.             (void) OTSetSynchronous(ep);
  51.             (void) OTSetBlocking(ep);
  52.             (void) OTUseSyncIdleEvents(ep, true);
  53.             (void) OTInstallNotifier(ep, MyNotifier, 0);
  54.             (void) OTBind(ep, nil, nil);
  55.             
  56.             OTMemzero(&connectCall, sizeof(connectCall));
  57.             connectCall.addr.buf = (UInt8 *) &connectAddr;
  58.             connectCall.addr.len = OTInitDNSAddress(&connectAddr, gHTTPServer);
  59.             
  60.             err = OTConnect(ep, &connectCall, nil);
  61.             
  62.             if (err == noErr) {
  63.                 bytesSent = OTSnd(ep, gGetHTTPCommand, strlen(gGetHTTPCommand), 0);
  64.                 if ( bytesSent < 0 ) {
  65.                     err = bytesSent;
  66.                 }
  67.             }
  68.             if (err == noErr) {
  69.                 do {
  70.                     bytesReceived = OTRcv(ep, &ch, 1, &junkFlags);
  71.                     if (bytesReceived < 0) {
  72.                         err = bytesReceived;
  73.                     } else {
  74.                         printf("%c", ch);
  75.                     }
  76.                 } while (err == noErr);
  77.             }
  78.             
  79.             OTCloseProvider(ep);
  80.         }
  81.         CloseOpenTransport();
  82.     }
  83.     
  84.     if (err == noErr) {
  85.         printf("Success!\n");
  86.     } else {
  87.         printf("Failed with error %d.\n", err);
  88.     }
  89.     printf("Done.  Press command-Q to Quit.\n");
  90. }